package com.heima.schedule.feign;

import cn.hutool.core.util.ObjectUtil;
import com.heima.apis.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.schedule.dtos.Task;
import com.heima.schedule.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/11/25
 */
@RestController
public class ScheduleClient implements IScheduleClient {
    @Autowired
    private TaskService taskService;
    /**
     * 添加任务
     * @param task
     * @return
     */
    @Override
    @PostMapping("/api/v1/task/add")
    public ResponseResult addTask(@RequestBody Task task) {
        Long taskID = taskService.addTask(task);
        return ResponseResult.okResult(taskID);
    }

    /**
     * 取消任务
     * @param taskId
     * @return
     */
    @Override
    @GetMapping("/api/v1/task/cancel/{taskId}")
    public ResponseResult cancelTask(@PathVariable Long taskId) {
        Boolean result = taskService.cancelTask(taskId);
        return ResponseResult.okResult(result);
    }

    /**
     * 按照类型和优先级拉取任务
     * @param type
     * @param priority
     * @return
     */
    @Override
    @GetMapping("/api/v1/task/poll/{type}/{priority}")
    public ResponseResult poll(@PathVariable("type") Integer type,
                               @PathVariable("priority") Integer priority) {
        Task task = taskService.poll(type, priority);
        return ResponseResult.okResult(task);
    }
}